import 'package:flutter/material.dart';

const marginVal = EdgeInsets.symmetric(horizontal: 30, vertical: 80);

class NPopupRoute<T> extends PopupRoute<T> {
  NPopupRoute({
    this.backgroudColor,
    this.onClick,
    this.child,
    this.alignment = Alignment.center,
  });

  @override
  Color get barrierColor => null;

  @override
  bool get barrierDismissible => false;

  @override
  String get barrierLabel => null;

  @override
  Duration get transitionDuration => Duration(seconds: 0);

  /// backgroudColor
  final Color backgroudColor;

  /// child'alignment, default value: [Alignment.center]
  final Alignment alignment;

  /// child
  final Widget child;

  /// backgroudView action
  final Function onClick;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    final screenSize = MediaQuery.of(context).size;

    return Material(
      color: Colors.transparent,
      child: GestureDetector(
        onTap: () {
          if (onClick != null) onClick();
        },
        child: Stack(
          children: [
            Container(
              width: screenSize.width,
              height: screenSize.height,
              color: backgroudColor ?? Colors.black.withOpacity(0.3),
            ),
            SafeArea(
              child: Align(
                alignment: alignment,
                child: child,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class NAlertDialog extends StatelessWidget {
  final Widget header;

  final Widget content;

  final Widget footer;

  final EdgeInsets margin;

  final List<Widget> actions;

  final Widget cancelButton;

  final Widget confirmButton;

  final Color dividerColor;

  NAlertDialog({
    Key key,
    this.header,
    this.content,
    this.footer,
    this.margin = marginVal,
    this.actions = const [],
    this.cancelButton,
    this.confirmButton,
    this.dividerColor,
  })  : assert(header != null || content != null),
        assert(
            (actions != null || cancelButton != null || confirmButton != null)),
        super(key: key);

  Widget dtlCreateActions(Color _dividerColor) {
    if (this.actions.length > 0) {
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: this
            .actions
            .map(
              (e) => Expanded(
                child: e,
              ),
            )
            .toList(),
      );
    } else {
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (cancelButton != null)
            Expanded(
              child: cancelButton,
            ),
          Container(height: 55, child: VerticalDivider(color: _dividerColor)),
          if (confirmButton != null)
            Expanded(
              child: confirmButton,
            ),
        ],
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    Color _dividerColor = dividerColor ?? Theme.of(context).dividerColor;

    return Container(
      margin: margin,
      decoration: BoxDecoration(
        color: Colors.white,
        // color: Theme.of(context).scaffoldBackgroundColor,
        borderRadius: BorderRadius.circular((10.0)), // 圆角度
      ),
      child: Padding(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (header != null)
              Padding(
                padding: EdgeInsets.only(bottom: 16.0),
                child: header,
              ),
            if (content != null) content,
            if (footer != null) footer,
            Container(
              height: 0.5,
              color: _dividerColor,
            ),
            dtlCreateActions(_dividerColor),
          ],
        ),
        padding: EdgeInsets.all(12),
      ),
    );
  }
}
